<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
    margin: 0;
    padding: 0;
}
.tab{
    width: 600px;
    margin: 100px auto 0;
}
.options{
    overflow: hidden;
    border-top: 2px solid #06A86E;
}
.options h3{
    float: left;
    width: 150px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    background-color: #F7F8F8;
    font-size: 18px;
    font-weight: normal;
    color: #666;
    cursor: pointer;
}
.contents{
    height: 200px;
    padding: 10px 0;
    overflow: hidden;
}
.contents div{
    overflow: hidden;
    margin-bottom: 10px;
    display: none;
}
.contents div img{
    width: 150px;
    height: 200px;
    float: left;
}
/* 选中状态的样式 */
.options h3.active{
    color: #fff;
    background-color: #06A86E;
}
.contents div.show{
    display: block;
}
</style>
</head>
<body>
    
<!-- 最外层容器 -->
<div class="tab tab1">
    <!-- 选项区 -->
    <div class="options">
        <h3 class="active">科幻</h3>
        <h3>爱情</h3>
        <h3>动作</h3>
        <h3>喜剧</h3>
    </div>
    <!-- 内容区 -->
    <div class="contents">
        <div class="show">
            <img src="./imgs/e1.png" alt="">
            <img src="./imgs/e1.png" alt="">
            <img src="./imgs/e1.png" alt="">
            <img src="./imgs/e1.png" alt="">
        </div>
        <div>
            <img src="./imgs/e2.png" alt="">
            <img src="./imgs/e2.png" alt="">
            <img src="./imgs/e2.png" alt="">
            <img src="./imgs/e2.png" alt="">
        </div>
        <div>
            <img src="./imgs/e3.png" alt="">
            <img src="./imgs/e3.png" alt="">
            <img src="./imgs/e3.png" alt="">
            <img src="./imgs/e3.png" alt="">
        </div>
        <div>
            <img src="./imgs/e4.png" alt="">
            <img src="./imgs/e4.png" alt="">
            <img src="./imgs/e4.png" alt="">
            <img src="./imgs/e4.png" alt="">
        </div>
    </div>
</div>

<!-- 最外层容器 -->
<div class="tab tab2">
    <!-- 选项区 -->
    <div class="options">
        <h3 class="active">科幻</h3>
        <h3>爱情</h3>
        <h3>动作</h3>
        <h3>喜剧</h3>
    </div>
    <!-- 内容区 -->
    <div class="contents">
        <div class="show">
            <img src="./imgs/e1.png" alt="">
            <img src="./imgs/e1.png" alt="">
            <img src="./imgs/e1.png" alt="">
            <img src="./imgs/e1.png" alt="">
        </div>
        <div>
            <img src="./imgs/e2.png" alt="">
            <img src="./imgs/e2.png" alt="">
            <img src="./imgs/e2.png" alt="">
            <img src="./imgs/e2.png" alt="">
        </div>
        <div>
            <img src="./imgs/e3.png" alt="">
            <img src="./imgs/e3.png" alt="">
            <img src="./imgs/e3.png" alt="">
            <img src="./imgs/e3.png" alt="">
        </div>
        <div>
            <img src="./imgs/e4.png" alt="">
            <img src="./imgs/e4.png" alt="">
            <img src="./imgs/e4.png" alt="">
            <img src="./imgs/e4.png" alt="">
        </div>
    </div>
</div>

<script>

/* // 面向过程：
// 获取元素
var h3s = document.querySelectorAll('.options h3');
var divs = document.querySelectorAll('.contents div');
var prevIndex = 0;// 记录上次选中项的索引

// 遍历给每一个h3选项添加点击事件
for (var i = 0, len = h3s.length; i < len; i++) {
    // 给每一个h3节点添加一个index属性，保存它自己的索引值
    h3s[i].index = i;
    h3s[i].onclick = function (){
        // 给上次选中项去掉类名
        h3s[prevIndex].className = '';
        divs[prevIndex].className = '';
        
        // 给当前点击选项添加类名
        h3s[this.index].className = 'active';
        divs[this.index].className = 'show';
        
        // 更新上次选中项的索引
        prevIndex = this.index;
    }
} */


// 面向对象：模块化
(function (){
    // 构造函数
    function Tab(selector1,selector2){
        this.h3s = this.$(selector1);
        this.divs = this.$(selector2);
        this.prevIndex = 0;
    }
    // 获取元素的方法
    Tab.prototype.$ = function (selector){
        return document.querySelectorAll(selector);
    }
    // 添加事件的方法
    Tab.prototype.addEvent = function (){
        var _this = this;
        for (var i = 0; i < this.h3s.length; i++) {
            this.h3s[i].index = i;
            this.h3s[i].onclick = function (){
                _this.h3s[_this.prevIndex].className = '';
                _this.divs[_this.prevIndex].className = '';

                _this.h3s[this.index].className = 'active';
                _this.divs[this.index].className = 'show';

                _this.prevIndex = this.index;
            }
        }
    }

    // 对外暴露接口
    window.Tab = Tab;
})();

var tab1 = new Tab('.tab1 h3','.tab1 .contents div');
tab1.addEvent();

var tab2 = new Tab('.tab2 h3','.tab2 .contents div');
tab2.addEvent();
</script>
</body>
</html>